package com.jic.market.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.common.base.MarketBaseService;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.entity.PromotionTemplate;
import com.jic.market.mapper.PromotionTemplateMapper;
import com.jic.market.request.manager.template.TemplateDeleteRequest;
import com.jic.market.request.manager.template.TemplateEditRequest;
import com.jic.market.request.manager.template.TemplatePageQueryRequest;
import com.jic.market.service.PromotionTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 活动模板表
 * @author : 
 * @email : 
 * @since : 2020-03-12 06:46:27
 * @version : v1.0.0
 */
@Slf4j
@Service
public class PromotionTemplateServiceImpl extends MarketBaseService implements PromotionTemplateService{

    @Autowired
    private PromotionTemplateMapper promotionTemplateMapper;

    @Override
    public Long saveTemplate(OriginPromotionService originPromotionService) {
        // 调用复制方法
        CopyResult copyResult = originPromotionService.copy();
        // 校验
        if(null == copyResult || null == copyResult.getPromotionId()
                || null == copyResult.getPromType() || null == copyResult.getTemplateName()){
            log.error("复制结果为空，无法生成模板");
            return null;
        }
        // 模板名称校验
        PromotionTemplate promotionTemplateQ = new PromotionTemplate();
        promotionTemplateQ.setTemplateName(copyResult.getTemplateName());
        List<PromotionTemplate>  promotionTemplateQList = promotionTemplateMapper.query(promotionTemplateQ);
        if(null != promotionTemplateQList && !promotionTemplateQList.isEmpty()){
            log.error("模板名称已经存在");
            throw new MarketException(MarketErrorCodeEnum.TEMPLATE_NAME_EXIST_ERROR, "模板名称已经存在");
        }
        // 将新的活动id与模板表进行关联
        PromotionTemplate promotionTemplate = new PromotionTemplate();
        promotionTemplate.setPromotionId(copyResult.getPromotionId());
        promotionTemplate.setTemplateName(copyResult.getTemplateName());
        promotionTemplate.setPromType(copyResult.getPromType().getCode());
        if(null != copyResult.getAdminOperate()){
            promotionTemplate.setDeptId(copyResult.getAdminOperate().getDeptId());
            promotionTemplate.setDeptType(copyResult.getAdminOperate().getDeptType().getCode());
        }
        promotionTemplate.setDeleteFlag(false);
        promotionTemplate.setCreateTime(new Date());
        promotionTemplate.setCreator(copyResult.getAdminOperate().getOperator());
        promotionTemplate.setCreatorName(copyResult.getAdminOperate().getOperateName());
        long flag = promotionTemplateMapper.insert(promotionTemplate);
        if(0 != flag){
            return promotionTemplate.getId();
        }
        return null;
    }

    @Override
    public RestResult<PageResult<PromotionTemplate>> queryPage(TemplatePageQueryRequest request) {
        return executeQuery(() -> {
            //计算下标
            int startIndex = (request.getStart() - 1) * request.getLimit();
            PromotionTemplate promotionTemplate = new PromotionTemplate();
            BeanUtils.copyProperties(request, promotionTemplate);
            AdminOperate adminOperate = request.getAdminOperate();
            if(null != adminOperate){
                promotionTemplate.setDeptId(adminOperate.getDeptId());
                promotionTemplate.setDeptType(adminOperate.getDeptType().getCode());
                promotionTemplate.setDeleteFlag(false);
            }
            List<PromotionTemplate> list = promotionTemplateMapper.queryPage(startIndex, request.getLimit(), promotionTemplate);
            long count = promotionTemplateMapper.queryPageCount(promotionTemplate);
            PageResult pageResult = new PageResult();
            pageResult.setRows(list);
            pageResult.setTotal(count);
            return pageResult;
        });
    }

    @Override
    public RestResult<Long> edit(TemplateEditRequest request) {
        return executeQuery(() -> {
            PromotionTemplate promotionTemplate = new PromotionTemplate();
            promotionTemplate.setId(request.getId());
            promotionTemplate.setTemplateName(request.getTemplateName());
            promotionTemplate.setUpdateTime(new Date());
            return promotionTemplateMapper.update(promotionTemplate);
        });
    }

    @Override
    public RestResult<Long> delete(TemplateDeleteRequest request) {
        return executeQuery(() -> {
            return promotionTemplateMapper.delete(request.getId());
        });
    }

}